import request from '../utils/request'
import download from '../utils/download'

export function uploadFile (data: any, callback: any, messageObj: any) {
  return request({
    headers: { 'Content-Type': 'multipart/form-data;charset=utf-8' },
    url: '/wb/file/upload',
    method: 'post',
    onUploadProgress: function(progressEvent) {
      if (callback) {
        callback(messageObj, progressEvent);
      }
    },
    data
  })
}

export function downloadFile (data: any, callback: any, messageObj: any) {
  return download({
    url: '/wb/file/download',
    headers: { 'Content-Type': 'application/json;charset=UTF-8'},
    method: 'post',
    onDownloadProgress: function(progressEvent) {
      if (callback) {
        callback(messageObj, progressEvent);
      }
    },
    data
  })
}

export function downloadTaskFile (data: any, callback: any, messageObj: any) {
  return download({
    url: '/wb/task/download',
    headers: { 'Content-Type': 'application/json;charset=UTF-8'},
    method: 'post',
    onDownloadProgress: function(progressEvent) {
      if (callback) {
        callback(messageObj, progressEvent);
      }
    },
    data
  })
}

export function downloadUrlFile (url: string, callback: any, messageObj: any) {
  return download({
    url: url,
    method: 'get',
    onDownloadProgress: function(progressEvent) {
      if (callback) {
        callback(messageObj, progressEvent);
      }
    },
    params: {fileName: messageObj.name},
    responseType: 'blob'
  })
}

export function updateAvatar (data: any) {
  return request({
    headers: { 'Content-Type': 'multipart/form-data;charset=utf-8' },
    url: '/wb/file/avatar',
    method: 'post',
    data
  })
}
